import { Observable } from 'rxjs'
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'
import { IncomingMessage, ServerResponse } from 'http'

@Injectable()
export class PostInterceptor implements NestInterceptor {

  intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
    let req = context.switchToHttp().getRequest<IncomingMessage>()
    let res = context.switchToHttp().getResponse<ServerResponse>()
    if (req.method.toLowerCase() == 'post' && res.statusCode === 201) {
      res.statusCode = 200
    }
    return next.handle()
  }

}
